Skip to content

Support expiring oauth tokens for better security - #5688

Closed
stephanmiehe wants to merge 1 commit into
hacs:mainfrom
stephanmiehe:feat/expiring-oauth-tokens
Closed

stephanmiehe wants to merge 1 commit into
hacs:mainfrom
stephanmiehe:feat/expiring-oauth-tokens

Conversation

@stephanmiehe

@stephanmiehe stephanmiehe commented Nov 12, 2026 •

Copy link
Copy Markdown

Summary

Add expiring GitHub OAuth tokens with automatic refresh, safe rotation and shutdown handling, and reauthentication when needed. Preserve existing non-expiring logins and redact refresh tokens from diagnostics.

Using expiring tokens ensures that should a token ever leak, the exposure is limited by the lifetime of the oauth token. There is an influx in security incidents going on around the world because threat actors are finding oauth tokens and weaponizing them.

Validation

Regression tests and lint pass. Full suite: 579 passed, followed by a Python shutdown crash also reproduced on the unchanged baseline.

Rollout

Eventually, you'll want to set the recently introduced config flag in your GitHub Oauth app that forces all tokens to be expiring. However, you want to give people some time to upgrade before you do that since once you flip that switch, users will need to re-auth using a version of HACS that has the token renewal logic being added in this PR

Copilot AI balanced review requested due to automatic review settings November 12, 2026 02:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An in-flight request can fail after token rotation and incorrectly disable HACS and trigger reauthentication, because authentication failures are not retried with the newly valid credential.

1 open issue
What changed in this PR

Adds expiring GitHub OAuth credentials to HACS, with automatic refresh while preserving existing non-expiring logins.

Changes:

  • Stores token lifetimes and coordinates refresh, rotation, unload, and shutdown.
  • Updates both GitHub clients without reloading HACS and redacts refresh tokens from diagnostics.
  • Adds regression tests and API-usage snapshots for refresh and reauthentication.
File Description
tests/​test_config_flow.py Tests expiring-token login, reauthentication, and token-only updates.
tests/​test_auth.py Tests rotation, failures, concurrency, shutdown, and client updates.
tests/​snapshots/​api-usage/​tests/​test_config_flowtest-refreshable-device-flow-true.json Records reauthentication API usage.
tests/​snapshots/​api-usage/​tests/​test_config_flowtest-refreshable-device-flow-false.json Records initial authorization API usage.
tests/​snapshots/​api-usage/​tests/​test_authtest-terminal-failure-starts-existing-reauth-flow.json Records terminal refresh-failure API usage.
tests/​snapshots/​api-usage/​tests/​test_authtest-refreshed-credentials-reach-both-clients-without-reload.json Records API usage after client credential updates.
custom_components/​hacs/​system_health.py Routes health requests through refresh-aware handling.
custom_components/​hacs/​repositories/​base.py Resolves current credentials before legacy repository requests.
custom_components/​hacs/​diagnostics.py Uses refresh-aware requests and redacts refresh tokens.
custom_components/​hacs/​config_flow.py Requests offline access and stores complete token metadata.
custom_components/​hacs/​base.py Synchronizes client credentials before API calls.
custom_components/​hacs/​auth.py Adds token validation and coordinated refresh management.
custom_components/​hacs/​__init__.py Integrates token-manager lifecycle and avoids token-only reloads.

Review effort: Balanced

Comment on lines +514 to 516
if self.auth is not None:
await self.async_get_github_token()
return await method(*args, **kwargs)
@ludeeus

ludeeus commented Nov 12, 2026

Copy link
Copy Markdown
Member

Hi there 👋

HACS is moving to Home Assistant as the Marketplace, so no changes will be made to this integration.
See #5590 and https://hacs.xyz/docs/marketplace/

If your PR is still needed in the Marketplace integration, check out the Home Assistant contributor documentation for how to contribute there.

Closing this one.

@ludeeus ludeeus closed this Nov 12, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants